# Maintainer: Naveen M K <naveen521kk at gmail.com>

_realname=skia
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=108.0.5359.95
# blindly follow chromium version number
# see https://omahaproxy.appspot.com/all.json?os=win&channel=stable
# for the commit
_commit="7c55be996a81ff9c5c66984c9d4ef85d12a44c8c"
pkgrel=2
pkgdesc="Skia is a complete 2D graphic library for drawing Text, Geometries, and Images. (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://skia.org"
license=("spdx:BSD-3-Clause")
makedepends=(
    "${MINGW_PACKAGE_PREFIX}-python"
    "${MINGW_PACKAGE_PREFIX}-ninja"
    "${MINGW_PACKAGE_PREFIX}-cc"
    "${MINGW_PACKAGE_PREFIX}-pkg-config"
    "${MINGW_PACKAGE_PREFIX}-gn"
    "git"
)
depends=(
    "${MINGW_PACKAGE_PREFIX}-expat"
    "${MINGW_PACKAGE_PREFIX}-libpng"
    "${MINGW_PACKAGE_PREFIX}-zlib"
    "${MINGW_PACKAGE_PREFIX}-libwebp"
    "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
    "${MINGW_PACKAGE_PREFIX}-harfbuzz"
    "${MINGW_PACKAGE_PREFIX}-icu"
)
source=("${_realname}-${pkgver}.tar.gz::https://github.com/google/skia/archive/${_commit}.tar.gz"
    "bare-clones/build::git+https://chromium.googlesource.com/chromium/src/build.git#commit=fd86d60f33cbc794537c4da2ef7e298d7f81138e"
    "0001-Don-t-download-prebuild-gn-and-emsdk.patch"
    "0002-add-mingw-toolchain-skia.patch"
    "0003-fix-dwrite-function-mingw.patch"
    "0004-add-mingw-toolchain-build.patch"
    "0005-dont-undef-CLSID_WICImagingFactory-on-mingw.patch"
    "0006-fix-std-max.patch"
    "harfbuzz.gn"
    "icu.gn"
    "zlib.gn"
    "skia.pc"
)
noextract=("${_realname}-${pkgver}.tar.gz")
sha256sums=('48ff8b020d2e54b97ddf7cfc54bb483492aef3dc7b3e61629c8a86bc89995643'
            'SKIP'
            '5df0992b78f7f6bedba8cef110c73c723513d032015e9b756decb0a06a99a2f6'
            '518166cb8c4848deee114c0f38c0c80289f6d170a31dbaacd0455a573599cd5b'
            '76592221a23555a29debc77df7e2429d52e2b7db1c103e0785bb7a4c669b9e03'
            '63f35c12168fcb5f025db2cb3a4bc693cb4546ce561dc95e45d10c1579daf786'
            '7883ff95984f5eac3002d8205257666f38538e0907046ef17e4850a36103a12c'
            '96b98caea6c426ff0b4f85ced7b45d4b40777396ac7b12bb6490b9d387c1b3c8'
            '7bd0b8cff2f1ec6822c74b731664007f598c3b442718060c499df7ba19141563'
            '36d32b737f87a676f5c4da9b7ae14193e6c1913ddb6508b979effdba2b80ea83'
            'b99874ea904724911528a5530c14f3051c358805bd36df9d205df31458ebf8b9'
            'a5dd30a1b53609aad427945fd119709a899d23a2e9606fbb676c146fdd04e3cd')

apply_patch_with_msg() {
    for _patch in "$@"; do
        msg2 "Applying $_patch"
        patch -Nbp1 -i "${srcdir}/$_patch"
    done
}

prepare() {
    tar -xf "${_realname}-${pkgver}.tar.gz" || true
    mv "${srcdir}/skia-${_commit}" "${srcdir}/skia"

    echo "Syncing all dependencies"
    cd "${srcdir}/skia"
    apply_patch_with_msg 0001-Don-t-download-prebuild-gn-and-emsdk.patch
    python ${srcdir}/skia/tools/git-sync-deps

    echo "Patching build directory"
    cd "${srcdir}"/build
    apply_patch_with_msg 0004-add-mingw-toolchain-build.patch
    #python "${srcdir}"/build/util/lastchange.py -o "${srcdir}"/build/util/LASTCHANGE
    rm -rf "${srcdir}"/skia/build
    ln -sf "${srcdir}"/build "${srcdir}"/skia
    echo "checkout_google_benchmark = false" >"${srcdir}"/skia/build/config/gclient_args.gni

    echo "Preparing Skia Source"
    cd "${srcdir}"/skia
    apply_patch_with_msg 0002-add-mingw-toolchain-skia.patch
    apply_patch_with_msg 0003-fix-dwrite-function-mingw.patch
    apply_patch_with_msg 0005-dont-undef-CLSID_WICImagingFactory-on-mingw.patch
    apply_patch_with_msg 0006-fix-std-max.patch
    cp "${srcdir}/harfbuzz.gn" "${srcdir}/skia/third_party/harfbuzz/BUILD.gn"
    cp "${srcdir}/icu.gn" "${srcdir}/skia/third_party/icu/BUILD.gn"
    cp "${srcdir}/zlib.gn" "${srcdir}/skia/third_party/zlib/BUILD.gn"

    sed "s/@VERSION@/$pkgver/g" -i "$srcdir/skia.pc"
    sed "s/@CFLAGS@//g" -i "$srcdir/skia.pc"
    sed "s|^prefix=.*|prefix=${MINGW_PREFIX}|g" -i "$srcdir/skia.pc"
}

build() {
    local BUILDTYPE=Release
    if check_option "debug" "y"; then
        BUILDTYPE="Debug"
    fi

    case ${CARCH} in
    i686)
        _arch=x86
        ;;
    x86_64)
        _arch=x64
        ;;
    esac
    case ${BUILDTYPE} in
    Release)
        _is_official=true
        ;;
    Debug)
        _is_official=false
        ;;
    esac

    case ${MSYSTEM} in
    CLANG64|CLANG32)
        export CXX=clang++
        export CC=clang
        export LD=clang++
        export AR=llvm-ar
    ;;
    *)
        export CXX=g++
        export CC=gcc
        export LD=g++
        export AR=ar
    ;;
    esac

    cd "${srcdir}"/skia
    ${MINGW_PREFIX}/bin/gn gen "${PWD}/out/${BUILDTYPE}-${CARCH}" --args="
      target_cpu=\"${_arch}\"
      is_official_build=${_is_official}
      cc=\"${CC}\"
      cxx=\"${CXX}\"
      is_component_build=false
      skia_enable_tools=false
      skia_use_system_libjpeg_turbo=true
      skia_use_system_libwebp=true
      skia_use_system_libpng=true
      use_sysroot=false
      skia_use_xps=false"

    ninja -C "${PWD}/out/${BUILDTYPE}-${CARCH}"
}

package() {
    cd "skia"
    local BUILDTYPE=Release
    if check_option "debug" "y"; then
        BUILDTYPE="Debug"
    fi
    ls "${PWD}/out/${BUILDTYPE}-${CARCH}"

    install -d "$pkgdir"/${MINGW_PREFIX}/lib
    install -Dm755 out/${BUILDTYPE}-${CARCH}/*.a "$pkgdir"/${MINGW_PREFIX}/lib/

    install -d "$pkgdir"/${MINGW_PREFIX}/include/skia
    cp --parents `find ./include -name \*.h` "$pkgdir"/${MINGW_PREFIX}/include/skia
    cp --parents `find ./modules -name \*.h` "$pkgdir"/${MINGW_PREFIX}/include/skia

    install -d "$pkgdir"/${MINGW_PREFIX}/lib/pkgconfig
    install -m644 "$srcdir/skia.pc" "$pkgdir"/${MINGW_PREFIX}/lib/pkgconfig/

    install -d "$pkgdir"/${MINGW_PREFIX}/share/licenses/skia
    install -m644 LICENSE* "$pkgdir"/${MINGW_PREFIX}/share/licenses/${_realname}
}
